﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

namespace IAATO
{
    public partial class ZoneManagement : System.Web.UI.Page
    {
        private ZoneService zoneService;

        protected void Page_Load(object sender, EventArgs e)
        {
            zoneService = new ZoneService(new ZoneManager());

            #region binding gridView

            List<ZONE> zones = zoneService.getAllZones();
            if (zones == null)
            {
                lblError.Text = "An error has occurred";
            }
            else if (zones.Count == 0)
            {
                lblError.Text = "No area in the database";
                gdwZones.DataBind();
            }
            else
            {
                lblError.Text = "";
                gdwZones.DataSource = zones;
                gdwZones.DataBind();
            }

            #endregion
        }

        protected void gdwZones_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void btnAjouter_Click(object sender, EventArgs e)
        {
            zoneService.createZone(txtNom.Text);

            Response.Redirect("ZoneManagement.aspx");
        }

        protected void btnModifier_Click(object sender, EventArgs e)
        {
            zoneService.updateZone(int.Parse(idUpdate.Value), txtNom.Text);

            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            Response.Redirect("ZoneManagement.aspx");
        }

        protected void btnAnnuler_Click(object sender, EventArgs e)
        {
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;
            txtNom.Text = "";
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            zoneService.deleteZone(int.Parse(((ImageButton)sender).CommandArgument));

            Response.Redirect("ZoneManagement.aspx");
        }

        protected void update_Click(object sender, EventArgs e)
        {
            ZONE zone = zoneService.getZoneById(int.Parse(((ImageButton)sender).CommandArgument));

            txtNom.Text = zone.NOM;
            btnAjouter.Visible = false;
            btnModifier.Visible = true;
            btnAnnuler.Visible = true;

            idUpdate.Value = "" + zone.ID;

            hdfScrollIntoView.Value = "True";
        }

        protected void gdwZones_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdwZones.DataSource = zoneService.getAllZones();
            gdwZones.PageIndex = e.NewPageIndex;
            gdwZones.DataBind(); 
        }
    }
}